﻿#include <QCoreApplication>
#include "json/json.h"
#include <iostream>
#include <fstream>
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    std::ifstream ifs;
    std::ofstream ofs;

    //写json
    Json::FastWriter writer;
    Json::Value root;
    Json::Value tmpVal;
    root["id"] = "123456789";
    tmpVal["value_type"] = true;
    tmpVal["value"] = 123;
    tmpVal["value1"] = INT32_MAX;
    tmpVal["fvalue"] = 456.789;
    tmpVal["time"] = "2021-03-17 01:13:34.123";
    root["employees"].append(tmpVal);
    std::string strJson = writer.write(root);
    ofs.open("test.json");
    ofs << strJson;
    ofs.close();

    //读Json
    ifs.open("test.json");
    if(!ifs.is_open())
    {
        std::cout<<"open file error!" <<std::endl;
        return -1;
    }

    std::string str_;
    bool bbbb;
    int iii;
    unsigned int uiui;
    double fff;

    Json::Reader reader;
    reader.parse(ifs,root,false);
    if(!root.isObject())
    {
        return -1;
    }
    for(auto iter = root.begin(); iter != root.end(); ++iter)
    {
        Json::Value tmp = *iter;
        switch (tmp.type())
        {
        case Json::intValue:
            iii = tmp.asInt();
            break;
        case Json::uintValue:
            uiui = tmp.asUInt();
            break;
        case Json::realValue:
            uiui = tmp.asDouble();
            break;
        case Json::stringValue:
            str_ = tmp.asString();
            break;
        case Json::booleanValue:
            bbbb = tmp.asBool();
            break;
        case Json::arrayValue:
            for(auto it = tmp.begin(); it != tmp.end(); ++it)
            {
                Json::Value t = *it;
                switch (t.type())
                {
                case Json::intValue:
                    iii = t.asInt();
                    break;
                case Json::uintValue:
                    uiui = t.asUInt();
                    break;
                case Json::realValue:
                    uiui = t.asDouble();
                    break;
                case Json::stringValue:
                    str_ = t.asString();
                    break;
                case Json::booleanValue:
                    bbbb = t.asBool();
                    break;
                case Json::objectValue:
                    for(auto it0 = t.begin(); it0 != t.end(); ++it0)
                    {
                        Json::Value tt = *it0;
                        switch (tt.type())
                        {
                        case Json::intValue:
                            iii = tt.asInt();
                            break;
                        case Json::uintValue:
                            uiui = tt.asUInt();
                            break;
                        case Json::realValue:
                            fff = tt.asDouble();
                            break;
                        case Json::stringValue:
                            str_ = tt.asString();
                            break;
                        case Json::booleanValue:
                            bbbb = tt.asBool();
                            break;
                        }
                    }
                    break;
                }
            }
            break;
        default:
            break;
        }
    }
//    std::string str_type = root["value_type"].asString();
//    int val = root["value"].asInt();

    return a.exec();
}
